#include "../include/client.h"
#include <crypt.h>
/* Usage*/
 /* typedef struct clientLogTain_s{ */
 /*         int length; */
 /*         char data[1000]; */
 /*     }clientLogTrain_t; */
 


int sign_or_land(int sockfd)
{
    train_t train;
    bzero(&train,sizeof(train));
    /* printf("欢迎进入云服务系统\n"); */
    printf("请输入用户名：");
    char name[128]={0};
    int ret = read(STDIN_FILENO,name,sizeof(name));
    ERROR_CHECK(ret,-1,"read");
    train.len = ret;
    memcpy(train.data,name,ret);
    ret = send(sockfd,&train.len,sizeof(train.len),MSG_NOSIGNAL);
    ERROR_CHECK(ret,-1,"send");
    ret = send(sockfd,train.data,train.len,MSG_NOSIGNAL);
    ERROR_CHECK(ret,-1,"send");
  
    char buf[1024]={0};
    bzero(&train,sizeof(train));
    ret=recv(sockfd,&train.len,sizeof(train.len),MSG_WAITALL);
    ERROR_CHECK(ret,-1,"recv");
    ret=recv(sockfd,train.data,train.len,MSG_WAITALL);
    ERROR_CHECK(ret,-1,"recv");
  
    bzero(buf,sizeof(buf));
    memcpy(buf,train.data,train.len);
    printf("%s\n",buf);//登录或者注册
    bzero(&train,sizeof(train));
    ret=recv(sockfd,&train.len,sizeof(train.len),MSG_WAITALL);
    ERROR_CHECK(ret,-1,"recv");
    ret=recv(sockfd,train.data,train.len,MSG_WAITALL);
    ERROR_CHECK(ret,-1,"recv");
    bzero(buf,sizeof(buf));
    memcpy(buf,train.data,train.len);//储存颜值
    char password[1024]={0};
    while(1){

        bzero(password,sizeof(password));
        bzero(&train,sizeof(train));
        ret = read(STDIN_FILENO,password,sizeof(password));
        char *new_crypt = crypt(password,buf);//加密
        train.len = strlen(new_crypt);
        memcpy(train.data,new_crypt,train.len);
        send(sockfd,&train.len,sizeof(train.len),MSG_NOSIGNAL);
        send(sockfd,train.data,train.len,MSG_NOSIGNAL);//发送密文密码
        bzero(&train,sizeof(train));
        ret = recv(sockfd,&train.len,sizeof(train.len),MSG_WAITALL);
        ERROR_CHECK(ret,-1,"recv");
        ret = recv(sockfd,train.data,train.len,MSG_WAITALL);//接收反馈
        ERROR_CHECK(ret,-1,"recv");
        //密码正确----
        //密码错误--
        if(strcmp(train.data,"登录成功！")==0)
        {
            printf("欢迎进入云服务系统\n");
            return 0;
        } else{
            printf("密码输入错误 请重新输入密码\n");
            continue;
        }
}}







/* int sendLogin(int sockfd) */
/* { */ 
/*     clientLogTrain_t train; */
/*     int login_succss = 0; */
/*     char sendName[1024]; */
/*     while(login_succss == 0){ */

/*         printf("请输入用户名：\n"); */
/*         fgets(sendName,sizeof(sendName),stdin); */
/*         sendName[strlen(sendName)-1] = '\0'; */
    
/*         train.length = strlen(sendName) + 1; */
/*         strcpy(train.data,sendName); */
/*         send(sockfd,&train.length,sizeof(train.length),0); */
/*         send(sockfd,train.data,train.length,0); */

/*         //接受用户是否存在 */
/*         bzero(&train,sizeof(train)); */
/*         recv(sockfd,&train.length,sizeof(train.length),0); */
/*         recv(sockfd,train.data,train.length,0); */

/*         if(strcmp(train.data,"用户不存在！") == 0){ */ 
/*             printf("用户不存在，请重新输入\n"); */
/*             continue; */
/*         } else if(strcmp(train.data,"用户存在！") == 0){ */ 
/*             char pswdBuf[1024]; */
/*             printf("用户存在,"); */

/*             //接收服务端发来的盐值 */
/*             bzero(&train,sizeof(train)); */
/*             char salt[4096]; */
/*             recv(sockfd,&train.length,sizeof(train.length),0); */
/*             recv(sockfd,train.data,train.length,0); */
/*             strcpy(salt,train.data); */

/*             int pswd_succ = 0; */
/*             while(pswd_succ == 0 && login_succss == 0){ */  
/*                 printf("请输入密码：\n"); */
/*                 fflush(stdin); */
/*                 fgets(pswdBuf,sizeof(pswdBuf),stdin); */
/*                 pswdBuf[strlen(pswdBuf)-1] = '\0'; */


/*                 char *new_crypt = crypt(pswdBuf, salt); */
/*                 /1* printf("new_crypt =%s\n",new_crypt);//测试 *1/ */
                
/*                 //发送加密的密文密码 */
/*                 bzero(&train,sizeof(train)); */
/*                 train.length = strlen(new_crypt) + 1; */
/*                 strcpy(train.data,new_crypt); */
/*                 send(sockfd,&train.length,sizeof(train.length),0); */
/*                 send(sockfd,train.data,train.length,0); */

/*                 //接收登陆是否成功的火车 */
/*                 bzero(&train,sizeof(train)); */
/*                 char sucMsg[1024]; */
/*                 recv(sockfd,&train.length,sizeof(train.length),0); */
/*                 recv(sockfd,train.data,train.length,0); */
/*                 strcpy(sucMsg,train.data); */

/*                 if(strcmp(sucMsg,"登陆成功！") == 0) */
/*                 { */ 
/*                     printf("%s\n",sucMsg); */
/*                     pswd_succ = 1; */
/*                     login_succss = 1; */
/*                     fflush(stdin); */
/*                     return 1; */
/*                 } */
/*                 else if(strcmp(sucMsg,"尝试次数已用完！")==0) */
/*                 { */ 
/*                     printf("%s\n", sucMsg); */
/*                     printf("请重新登录！\n"); */
/*                     break;  // 跳出密码循环，回到用户名输入 */
/*                 } */
/*                 else{ */ 
/*                     printf("密码错误，请重新输入：\n"); */
/*                 } */
/*             } */
/*         } */
/*     } */
/*     fflush(stdin); */
/*     return 0; */
/* } */

/* /1* int main(int argc,char *argv[]) *1/ */
/* { */
/*    ARGS_CHECK(argc,3); */
/*    int sockfd = socket(AF_INET,SOCK_STREAM,0); */
/*    ERROR_CHECK(sockfd,-1,"socket"); */
/*    struct sockaddr_in addr; */
/*    addr.sin_family = AF_INET; */
/*    addr.sin_port = htons(atoi(argv[2])); */
/*    addr.sin_addr.s_addr = inet_addr(argv[1]); */

/*    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr)); */
/*    ERROR_CHECK(ret,-1,"connect"); */

/*    sendLogin(sockfd); */
/*    close(sockfd); */

/*     return 0; */
/* } */

